package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.DepartmentEntity;
import com.cheng.data.mapper.DepartmentMapper;
import com.cheng.service.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/10 8:49
 * @version: 1.0
 * @description: 有关 department 部门信息表的操作
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    private final static Logger log = LoggerFactory.getLogger(DepartmentServiceImpl.class);

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: java.util.List<com.cheng.data.entity.DepartmentEntity>
     * @date: 2021/11/11
     * @description: 查询 department 列表信息
     */
    @Override
    public List<DepartmentEntity> selectDepartmentInfo(DepartmentEntity entity) throws CommonException{

        QueryWrapper<DepartmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(entity.getDtId()),"dt_Id",entity.getDtId());
        wrapper.eq(StringUtil.isNotNull(entity.getDtTypeCode()),"dt_type_code",entity.getDtTypeCode());
        wrapper.like(StringUtil.isNotNull(entity.getDtTypeNm()),"dt_type_nm",entity.getDtTypeNm());
        wrapper.eq(StringUtil.isNotNull(entity.getDtUpsTypeCode()),"dt_ups_type_code",entity.getDtUpsTypeCode());
        wrapper.orderByDesc("dt_Id");
        return departmentMapper.selectList(wrapper);
    }

    /**
     * @author: ChengJW
     * @params: [departmentVO]
     * @return: com.cheng.common.results.DataRslt
     * @date: 2021/11/11
     * @description: 新增部门信息
     */
    @Override
    public int insertDepartmentInfo(DepartmentEntity entity) throws CommonException {
        return departmentMapper.insert(entity);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/12
    * @description: 修改部门信息
    */
    @Override
    public int updateDepartmentInfo(DepartmentEntity entity) throws CommonException {
        String dtId = null;
        if (null != entity) {
            dtId = entity.getDtId();
            //先通过主键查询部门信息，再替换修改的部分，其实只能修改部门名称，其他的不能修改。
            DepartmentEntity departmentEntity = departmentMapper.selectById(dtId);
            departmentEntity.setDtTypeCode(entity.getDtTypeCode());
            departmentEntity.setDtTypeNm(entity.getDtTypeNm());
            departmentEntity.setDtUpsTypeCode(entity.getDtUpsTypeCode());
            departmentEntity.setUtDt(new Date());
            return departmentMapper.updateById(departmentEntity);
        }
        return 0;
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/12
    * @description: 删除部门信息
    */
    @Override
    public int deleteDepartmentInfo(DepartmentEntity entity) throws CommonException {
        return departmentMapper.deleteById(entity.getDtId());
    }

    /**
     * @author: ChengJW
     * @params: [dpTypeCode]
     * @return: java.lang.String
     * @date: 2021/11/14
     * @description: 根据部门编号查询部门名称
     */
    @Override
    public String queryDepartmentNmByCode(String dpTypeCode) throws CommonException {

        DepartmentEntity departmentEntity = new DepartmentEntity();
        departmentEntity.setDtTypeCode(dpTypeCode);
        List<DepartmentEntity> list = selectDepartmentInfo(departmentEntity);
        if (null != list && list.size() > 0){
            return list.get(0).getDtTypeNm();
        }
        return null;
    }
}
